/*///////////////////////////////////////////////////////////
*
* FILE:		common.h
* AUTHOR:	Paul Spangler and Tuan Dang
* PROJECT:	CS 3251 Project 2 - Professor Traynor
* DESCRIPTION:	Common definitions for server and client
*
*////////////////////////////////////////////////////////////

#ifndef _COMMON_H_
#define _COMMON_H_

/* The size of the buffer when reading data. */

#define BUFSIZE 1024

/* The message indicators. */

#define MESSAGE_UPDATE '0'
#define MESSAGE_FRIENDS '1'
#define MESSAGE_HISTORY '2'
#define MESSAGE_LEAVE '3'

/* Struct representing GPS coordinates. */

typedef struct Coordinate
{
	float lattitude;
	float longitude;
} Coordinate;

/* Struct used to hold state while reading data. */

typedef struct ReadData
{
	int socket;
	ssize_t bufferSize;
	ssize_t dataLocation;
	char buffer[BUFSIZE];
} ReadData;

/**
 * Initializes a ReadData structure for a socket.
 */
ReadData read_init(int socket);

/**
 * Reads a byte from the data into the location pointed to by c.
 * Returns nonzero on success.
 */
int read_byte(ReadData *rd, char *c);

/**
 * Reads dataLen bytes into the location pointed to by data.
 * Returns nonzero on success.
 */
int read_data(ReadData *rd, void *data, size_t dataLen);

/**
 * Reads characters into the location pointed to by str until
 * a newline or tab character is reached, or dataLen characters have been
 * read. If dataLen is reached, the buffer is flushed until it reaches a
 * tab or newline. A null will not be written into str, but the delimiting
 * tab or newline will.
 *
 * Returns the number of characters read, not including the delimiter,
 * dataLen on truncation (str is full), or -1 on error.
 */
int read_string(ReadData *rd, char *str, size_t dataLen);

#endif
